package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotContactMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotContact;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 修改好友备注接口回调
 *
 * <pre>
 * {
 *     "nType": 3008,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcFriendWxId": "",
 *         "vcFriendSerialNo": "",
 *         "vcRemark": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _3008Handler implements CallbackHandler {

    @Resource
    private RobotContactMapper robotContactMapper;

    @Override
    public String name() {
        return "修改好友备注接口回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 3008;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();
        LambdaUpdateWrapper<RobotContact> wrapper = new LambdaUpdateWrapper<RobotContact>()
                .set(RobotContact::getRemark, data.getString("vcRemark"))
                .set(RobotContact::getUpdatedTime, now)
                .eq(RobotContact::getRobotId, callback.getRobotId())
                .eq(RobotContact::getContactId, data.getString("vcFriendWxId"));
        robotContactMapper.update(wrapper);
    }
}
